home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap16 / ShowDib7 / ShowDib7.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  7.3 KB  |  235 lines

  1. /*------------------------------------------
  2.    SHOWDIB7.C -- Shows DIB converted to DDB 
  3.                  (c) Charles Petzold, 1998
  4.   ------------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include "..\\ShowDib3\\PackeDib.h"
  8. #include "resource.h"
  9.  
  10. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  11.  
  12. TCHAR szAppName[] = TEXT ("ShowDib7") ;
  13.  
  14. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  15.                     PSTR szCmdLine, int iCmdShow)
  16. {
  17.      HWND     hwnd ;
  18.      MSG      msg ;
  19.      WNDCLASS wndclass ;
  20.  
  21.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  22.      wndclass.lpfnWndProc   = WndProc ;
  23.      wndclass.cbClsExtra    = 0 ;
  24.      wndclass.cbWndExtra    = 0 ;
  25.      wndclass.hInstance     = hInstance ;
  26.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  27.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  28.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  29.      wndclass.lpszMenuName  = szAppName ;
  30.      wndclass.lpszClassName = szAppName ;
  31.  
  32.      if (!RegisterClass (&wndclass))
  33.      {
  34.           MessageBox (NULL, TEXT ("This program requires Windows NT!"), 
  35.                       szAppName, MB_ICONERROR) ;
  36.           return 0 ;
  37.      }
  38.  
  39.      hwnd = CreateWindow (szAppName, TEXT ("Show DIB #7: Converted to DDB"),
  40.                           WS_OVERLAPPEDWINDOW,
  41.                           CW_USEDEFAULT, CW_USEDEFAULT,
  42.                           CW_USEDEFAULT, CW_USEDEFAULT, 
  43.                           NULL, NULL, hInstance, NULL) ;
  44.  
  45.      ShowWindow (hwnd, iCmdShow) ;
  46.      UpdateWindow (hwnd) ;
  47.  
  48.      while (GetMessage (&msg, NULL, 0, 0))
  49.      {
  50.           TranslateMessage (&msg) ;
  51.           DispatchMessage (&msg) ;
  52.      }
  53.      return msg.wParam ;
  54. }
  55.  
  56. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  57. {
  58.      static HBITMAP      hBitmap ;
  59.      static HPALETTE     hPalette ;
  60.      static int          cxClient, cyClient ;
  61.      static OPENFILENAME ofn ;
  62.      static TCHAR        szFileName [MAX_PATH], szTitleName [MAX_PATH] ;
  63.      static TCHAR        szFilter[] = TEXT ("Bitmap Files (*.BMP)\0*.bmp\0")
  64.                                       TEXT ("All Files (*.*)\0*.*\0\0") ;
  65.      BITMAP              bitmap ;
  66.      BITMAPINFO        * pPackedDib ;
  67.      HDC                 hdc, hdcMem ;
  68.      PAINTSTRUCT         ps ;
  69.  
  70.      switch (message)
  71.      {
  72.      case WM_CREATE:
  73.           ofn.lStructSize       = sizeof (OPENFILENAME) ;
  74.           ofn.hwndOwner         = hwnd ;
  75.           ofn.hInstance         = NULL ;
  76.           ofn.lpstrFilter       = szFilter ;
  77.           ofn.lpstrCustomFilter = NULL ;
  78.           ofn.nMaxCustFilter    = 0 ;
  79.           ofn.nFilterIndex      = 0 ;
  80.           ofn.lpstrFile         = szFileName ;
  81.           ofn.nMaxFile          = MAX_PATH ;
  82.           ofn.lpstrFileTitle    = szTitleName ;
  83.           ofn.nMaxFileTitle     = MAX_PATH ;
  84.           ofn.lpstrInitialDir   = NULL ;
  85.           ofn.lpstrTitle        = NULL ;
  86.           ofn.Flags             = 0 ;
  87.           ofn.nFileOffset       = 0 ;
  88.           ofn.nFileExtension    = 0 ;
  89.           ofn.lpstrDefExt       = TEXT ("bmp") ;
  90.           ofn.lCustData         = 0 ;
  91.           ofn.lpfnHook          = NULL ;
  92.           ofn.lpTemplateName    = NULL ;
  93.  
  94.           return 0 ;
  95.  
  96.      case WM_SIZE:
  97.           cxClient = LOWORD (lParam) ;
  98.           cyClient = HIWORD (lParam) ;
  99.           return 0 ;
  100.  
  101.      case WM_COMMAND:
  102.           switch (LOWORD (wParam))
  103.           {
  104.           case IDM_FILE_OPEN:
  105.  
  106.                     // Show the File Open dialog box
  107.  
  108.                if (!GetOpenFileName (&ofn))
  109.                     return 0 ;
  110.                
  111.                     // If there's an existing packed DIB, free the memory
  112.  
  113.                if (hBitmap)
  114.                {
  115.                     DeleteObject (hBitmap) ;
  116.                     hBitmap = NULL ;
  117.                }
  118.                
  119.                     // If there's an existing logical palette, delete it
  120.  
  121.                if (hPalette)
  122.                {
  123.                     DeleteObject (hPalette) ;
  124.                     hPalette = NULL ;
  125.                }
  126.                
  127.                     // Load the packed DIB into memory
  128.  
  129.                SetCursor (LoadCursor (NULL, IDC_WAIT)) ;
  130.                ShowCursor (TRUE) ;
  131.  
  132.                pPackedDib = PackedDibLoad (szFileName) ;
  133.  
  134.                ShowCursor (FALSE) ;
  135.                SetCursor (LoadCursor (NULL, IDC_ARROW)) ;
  136.  
  137.                if (pPackedDib)
  138.                {
  139.                          // Create palette from the DIB and select it into DC
  140.  
  141.                     hPalette = PackedDibCreatePalette (pPackedDib) ;
  142.  
  143.                     hdc = GetDC (hwnd) ;
  144.                     
  145.                     if (hPalette)
  146.                     {
  147.                          SelectPalette (hdc, hPalette, FALSE) ;
  148.                          RealizePalette (hdc) ;
  149.                     }
  150.                          // Create the DDB from the DIB
  151.  
  152.                     hBitmap = CreateDIBitmap (hdc,
  153.                                               (PBITMAPINFOHEADER) pPackedDib,
  154.                                               CBM_INIT,
  155.                                               PackedDibGetBitsPtr (pPackedDib),
  156.                                               pPackedDib,
  157.                                               DIB_RGB_COLORS) ;
  158.                     ReleaseDC (hwnd, hdc) ;
  159.  
  160.                          // Free the packed-DIB memory
  161.  
  162.                     free (pPackedDib) ;
  163.                }
  164.                else
  165.                {
  166.                     MessageBox (hwnd, TEXT ("Cannot load DIB file"), 
  167.                                 szAppName, 0) ;
  168.                }
  169.                InvalidateRect (hwnd, NULL, TRUE) ;
  170.                return 0 ;
  171.           }
  172.           break ;
  173.  
  174.      case WM_PAINT:
  175.           hdc = BeginPaint (hwnd, &ps) ;
  176.  
  177.           if (hPalette)
  178.           {
  179.                SelectPalette (hdc, hPalette, FALSE) ;
  180.                RealizePalette (hdc) ;
  181.           }
  182.           if (hBitmap)
  183.           {
  184.                GetObject (hBitmap, sizeof (BITMAP), &bitmap) ;
  185.  
  186.                hdcMem = CreateCompatibleDC (hdc) ;
  187.                SelectObject (hdcMem, hBitmap) ;
  188.  
  189.                BitBlt (hdc,    0, 0, bitmap.bmWidth, bitmap.bmHeight, 
  190.                        hdcMem, 0, 0, SRCCOPY) ;
  191.  
  192.                DeleteDC (hdcMem) ;
  193.           }
  194.           EndPaint (hwnd, &ps) ;
  195.           return 0 ;
  196.  
  197.      case WM_QUERYNEWPALETTE:
  198.           if (!hPalette)
  199.                return FALSE ;
  200.  
  201.           hdc = GetDC (hwnd) ;
  202.           SelectPalette (hdc, hPalette, FALSE) ;
  203.           RealizePalette (hdc) ;
  204.           InvalidateRect (hwnd, NULL, TRUE) ;
  205.  
  206.           ReleaseDC (hwnd, hdc) ;
  207.           return TRUE ;
  208.  
  209.      case WM_PALETTECHANGED:
  210.           if (!hPalette || (HWND) wParam == hwnd)
  211.                break ;
  212.  
  213.           hdc = GetDC (hwnd) ;
  214.           SelectPalette (hdc, hPalette, FALSE) ;
  215.           RealizePalette (hdc) ;
  216.           UpdateColors (hdc) ;
  217.  
  218.           ReleaseDC (hwnd, hdc) ;
  219.           break ;
  220.  
  221.           
  222.      case WM_DESTROY:
  223.           if (hBitmap)
  224.                DeleteObject (hBitmap) ;
  225.  
  226.           if (hPalette)
  227.                DeleteObject (hPalette) ;
  228.  
  229.           PostQuitMessage (0) ;
  230.           return 0 ;
  231.      }
  232.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  233. }
  234.  
  235.